﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;

namespace Kiosk.WebManagement
{
    public partial class FileManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadFiles();
        }

        private void LoadFiles()
        {
            List<string> temp = new List<string>(Directory.GetFiles(Server.MapPath("upload")));
            var files = from f in temp
                        where  IsImageOrVideo(f)
                        select new
                        {
                            FileName = f.Substring(f.LastIndexOf("\\") + 1, f.Length - 1 - f.LastIndexOf("\\"))
                        };
            grdFiles.DataSource = files;
            grdFiles.DataBind();
        }

        protected void grdFiles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string fileName = grdFiles.Rows[e.RowIndex].Cells[2].Text;
            try
            {
                File.Delete(Server.MapPath("~/upload/" + fileName));
            }
            catch (Exception ex)
            {
                throw;
            }
            LoadFiles();
        }

        protected void grdFiles_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            lblPreviewText.Visible = imgPreview.Visible = false;

            string fileName = grdFiles.Rows[e.NewSelectedIndex].Cells[2].Text;
            if (IsImage(fileName))
            {
                
                imgPreview.ImageUrl = "~/upload/" + fileName;
                imgPreview.Visible = true;
            }
            else
            {
                lblPreviewText.Visible = true;
            }
            
        }

        private bool IsImageOrVideo(string fileName)
        {
            bool isImage = false;

            try
            {
                string ext = fileName.Split('.')[1].ToLower();
                string extensions = "png,gif,bmp,jpg,jpeg,avi,wmv,mpg,mpeg";
                var result = from s in extensions.Split(',')
                             where s.Contains(ext)
                             select s;
                isImage = result.Count() > 0;
            }
            catch
            {
                isImage = false;
            }
            return isImage;
        }

        private bool IsImage(string fileName)
        {
            bool isImage = false;

            try
            {
                string ext = fileName.Split('.')[1].ToLower();
                string[] extensions = { "png","gif","bmp","jpg","jpeg" };
                var result = from s in extensions
                             where s.Contains(ext)
                             select s;
                isImage = result.Count() > 0;
            }
            catch
            {
                isImage = false;
            }
            return isImage;
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!fileUpload.HasFile)
            {
                lblFileWarning.Visible = true;
                lblFileWarning.Text = "You must specify a file to upload";
                return;
            }

            if (!IsImageOrVideo(fileUpload.FileName))
            {
                lblFileWarning.Visible = true;
                lblFileWarning.Text = "You cannot upload this type of file";
                return;
            }

            lblFileWarning.Visible = false;
            try
            {
                fileUpload.SaveAs(Server.MapPath("upload") + "\\" + fileUpload.FileName);
                LoadFiles();
            }
            catch (Exception ex)
            {
                lblFileWarning.Visible = true;
                lblFileWarning.Text = "Error uploading file " + ex.Message;
                
            }

        }
    }
}
